package com.luyuan.crm.sale.web;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.luyuan.crm.sale.model.FormulaOrder;
import com.luyuan.crm.sale.model.Order;
import com.luyuan.crm.sale.model.OrderRecord;
import com.luyuan.crm.sale.model.OrderStatus;
import com.luyuan.crm.sale.model.OrderType;
import com.luyuan.crm.sale.model.ProductOrder;
import com.luyuan.crm.sale.service.OrderRecordService;
import com.luyuan.crm.sale.service.OrderService;
import com.ruijie.blueprint.spring.context.ApplicationContextHolder;
import com.ruijie.components.security.model.AppUser;
import com.ruijie.components.security.model.Role;
import com.ruijie.web.jsf.action.BaseAction;
import com.ruijie.web.jsf.action.WizardAccessor;
import com.ruijie.foundation.security.AppUserContext;
import com.ruijie.util.StringUtils;

@Component("orderViewAction")
@Scope("view")
public class OrderViewAction extends BaseAction implements InitializingBean {

    private Order order;

    @Resource(name = "orderService")
    private OrderService orderService;

    @Resource(name = "orderRecordService")
    private OrderRecordService orderRecordService;

    private FormulaOrder formulaOrder;

    public DataModel getProductTableModel() {
        List<ProductOrder> products = new ArrayList<ProductOrder>();
        products.addAll(order.getProductOrders());
        Collections.sort(products);
        return new ListDataModel(products);
    }

    public DataModel getOrderRecordTableModel() {
        List<OrderRecord> orderRecords;
        orderRecords = orderRecordService.getRecordByOrder(order);
        return new ListDataModel(orderRecords);
    }

    public String startWizard() {
        WizardAccessor wizardAccessor = (WizardAccessor) ApplicationContextHolder.getApplicationContext().getBean(
                "wizardAccessor");
        setRequestAttribute("order", order, true);

        String url = "/luyuan/sale/orderViewForSale.jsf";
        AppUser appUser = (AppUser) AppUserContext.getCurrentUser();
        Set<Role> roles = appUser.getRoles();
        for (Role role : roles) {
            if ("核单员".equals(role.getRoleName())) {
                url = "/luyuan/sale/orderViewForChecker.jsf";
                break;
            }
        }
        if (order.getOrderType() == OrderType.SINGLE_PRODUCT) {
            return wizardAccessor.start("singleOrderWizard", "update", url + "?id=" + order.getId());
        }
        else {
            return wizardAccessor.start("orderWizard", "update", url + "?id=" + order.getId());
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        String id = getRequestParameter("id");
        if (StringUtils.isNullOrEmpty(id)) {
            id = (String) getRequestAttribute("id");
        }
        order = (Order) orderService.getById(id);
        if (order == null) {
            order = new Order();
            formulaOrder = new FormulaOrder();
        }
        else {
            formulaOrder = order.getFormulaOrder();
        }
    }

    public boolean isFormula() {
        if (order.getOrderType() == OrderType.FORMULA) {
            return true;
        }
        else {
            return false;
        }
    }

    public boolean isUpdatingStatus() {
        return order.getOrderStatus() == OrderStatus.UPDATING;
    }

    public boolean isDeliveredStatus() {
        return order.getOrderStatus() == OrderStatus.DELIVERED;
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    public FormulaOrder getFormulaOrder() {
        return formulaOrder;
    }

    public void setFormulaOrder(FormulaOrder formulaOrder) {
        this.formulaOrder = formulaOrder;
    }
}
